﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class QuotationEditControl : BaseEditControl {

        private FlexBindingSource<Quotation> _fbs;

        public override object BaseEditModel {
            get { return this._fbs.CurrentItem; }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<Quotation>(); }
                this._fbs.CurrentItem = (Quotation)value;
            }
        }

        public QuotationEditControl() {
            this.InitializeComponent();
        }

        private void QuotationEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<Quotation>(); }
            this.BindBasics(this.cmbUnit, BasicsType.UnitType);
            this.BindVendor();
            this.BindMaterial();
            this.CreateFormBindings();
            this.BindValidation();
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlVendor.DisplayMemberEx = "Code";
            this.ddlVendor.ValueMemberEx = "Key";
            this.ddlVendor.HeightEx = 300;
            this.ddlVendor.WidthEx = 400;
            this.ddlVendor.PageSizeEx = 20;
            this.ddlVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindMaterial() {
            MaterialSearchModel searchModel = new MaterialSearchModel() { IsEnabled = true };
            MaterialProcessor materialProcessor = new MaterialProcessor(int.MaxValue);
            List<MaterialViewModel> materialList = materialProcessor.FetchMaterialViewModelList(searchModel).ToList();

            this.ddlMaterial.DisplayMemberEx = "Code";
            this.ddlMaterial.ValueMemberEx = "Key";
            this.ddlMaterial.HeightEx = 300;
            this.ddlMaterial.WidthEx = 400;
            this.ddlMaterial.PageSizeEx = 20;
            this.ddlMaterial.DataBind(new MaterialViewModel(), x => new { x.Code, x.Name, x.MaterialType }, materialList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbPriceLessEqualThan, x => x.Text, y => y.Price);
            this._fbs.CreateBinding(this.tbPrice, x => x.Text, y => y.Price);
            this._fbs.CreateBinding(this.tbPriceGreaterThan, x => x.Text, y => y.PriceGreaterThan);
            this._fbs.CreateBinding(this.tbThresholdValue, x => x.Text, y => y.PriceThresholdValue); 
            this._fbs.CreateBinding(this.tbProcurementCycle, x => x.Text, y => y.ProcurementCycle);
            this._fbs.CreateBinding(this.tbPreProcurementCycle, x => x.Text, y => y.PreProcurementCycle);
            this._fbs.CreateBinding(this.cbIsMultiple, x => x.Checked, y => y.IsMultiple);
            this._fbs.CreateBinding(this.tbMinimumPackage, x => x.Text, y => y.MinimumPackage);
            this._fbs.CreateBinding(this.tbMinimumProcurement, x => x.Text, y => y.MinimumProcurement);
            this._fbs.CreateBinding(this.cmbUnit, x => x.SelectedValue, y => y.UnitId);
            this._fbs.CreateBinding(this.ddlVendor, x => x.SelectedValueEx, y => y.VendorId);
            this._fbs.CreateBinding(this.ddlMaterial, x => x.SelectedValueEx, y => y.MaterialId);
            this._fbs.CreateBinding(this.attachment, x => x.Text, y => y.AttachmentId, DataSourceUpdateMode.OnPropertyChanged);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbPriceLessEqualThan, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbPriceLessEqualThan, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbPriceLessEqualThan, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPriceLessEqualThan, "单价只能由数字组成");

            this.ValidatorForEdit.SetType(this.ddlVendor, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlVendor, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlMaterial, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlMaterial, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbUnit, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbUnit, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbProcurementCycle, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbProcurementCycle, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbProcurementCycle, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbProcurementCycle, "采购周期只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbPreProcurementCycle, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbPreProcurementCycle, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbPreProcurementCycle, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPreProcurementCycle, "采购前置周期只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbMinimumPackage, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMinimumPackage, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMinimumPackage, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMinimumPackage, "最小包装只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbMinimumProcurement, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMinimumProcurement, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMinimumProcurement, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMinimumProcurement, "最低采购量只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbThresholdValue, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbThresholdValue, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbThresholdValue, "阀值只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbPriceLessEqualThan, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbPriceLessEqualThan, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPriceLessEqualThan, "（<= 阀值）单价只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbPriceGreaterThan, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbPriceGreaterThan, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPriceGreaterThan, "（> 阀值）单价只能由数字组成");
        }
    }
}
